package com.zhibang.config.security;

import com.alibaba.fastjson2.JSON;
import com.zhibang.util.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 权限异常处理类
 **/
@Component
public class SecurityAccessException implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        Result<Object> r = Result.fail(401, "请先登录！！!");
        response.getWriter().print(JSON.toJSONString(r));
    }
}
